// var wueditor;
$(function () {
    wueditor = editormd("content-editormd", {//注意1：这里的就是上面的DIV的id属性值
        placeholder: '欢迎使用',
        width: "90%",
        height: 640,
        emoji: true,//emoji表情，默认关闭
        syncScrolling: "single",
        path: "/static/common/editor/lib/",//注意2：你的路径
        saveHTMLToTextarea: true,//注意3：这个配置，方便post提交表单
        // 图片上传
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "/upload/uploadFile"
    });
    uploadpic();
    tags();
});
$('#blog_add').on('click', function () {
    addBlog();
});
$('#testTag').on('click', function () {
  testme();
});

function addBlog() {
    var typ = $("#blog_category").val(),
        title = $("#blog_title").val(),
        lables = $("#blog_label").val(),
        img = $("#demoText").text(),
        tags = $("#tagValue").val(),
        markdownContent = wueditor.getMarkdown(),
        htmlContent = wueditor.getHTML(),
        allowed = allowed = $("input[name='allow_comment']:checked").val();
    datas = {};
    map = {};

    var user = localStorage.getItem("user");
    var token = localStorage.getItem("token");


    datas.postContentMd = markdownContent;
    datas.postContent = htmlContent;
    datas.allowComment = allowed;
    datas.postTitle = title;
    datas.postType = typ;
    datas.postSummary = tags+',';
    datas.postThumbnail = img;
    map.PostVo = datas;
    map.user = user;
    $.ajax({
        type: "POST",
        url: "/post/po",
        headers: {'Authorization': token},
        data: JSON.stringify(map),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',//预期服务器返回的数据类型
        beforeSend: function () {
            //让提交按钮失效
            $('#blog_add').prop('disabled', "true");
        },
        success: function (data) {
            console.log(data);
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    content: "操作成功！",//提示信息
                    success: function () {
                        layer.msg(data.msg, {time: 1 * 1000});
                    }
                });
                $("#resadd").click();
                $("#content-editormd-markdown-doc").val("");
            });

        },
        complete: function () {
            $('#blog_add').prop('disabled', false);
        }
    });
}

//上传图片
function uploadpic() {
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#imgupload',
            url: 'http://crawler_upload.6ack.com/upload/layedit',
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#demoText").html(res.data.src);
                console.log(res);
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
}
var tag = new Tag("tagValue");
function tags() {
    tag.initView();
}
function testme() {
   console.log($("#tagValue").val())
}